﻿using AlibabaCloud.SDK.Dingtalkcard_1_0.Models;
using GrapeCity.Forguncy.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tea;
using static dingtalk_Card.dingtalk_CardServerCommand;

namespace dingtalk_Card
{
    internal class toufang
    {
        //单聊酷应用场域投放参数
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.DeliverCardRequest.DeliverCardRequestImSingleOpenDeliverModel> danliaokuyingyong(IServerCommandExecuteContext dataContext, danliaokutoufang aaaa)
        {
            var bbb = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.DeliverCardRequest.DeliverCardRequestImSingleOpenDeliverModel();
            if (aaaa != null)
            {
                var UserIds = new Dictionary<string, string>();
                foreach (var item1 in aaaa.AT用户)
                {
                    // 先检查 param.参数名 是否为空
                    if (string.IsNullOrEmpty(Convert.ToString(item1.userId)))
                    {
                        dingcard.CreateErrorJson(108, "单聊酷应用投放参数：AT用户内容不能为空");
                        break; // 停止执行
                    }
                    // 将参数名和参数值转换为字符串并添加到字典中
                    var userId = (await dataContext.EvaluateFormulaAsync(item1.userId))?.ToString() ?? string.Empty;
                    var user = (await dataContext.EvaluateFormulaAsync(item1.用户名))?.ToString() ?? string.Empty;

                    if (userId != null && user != null)
                    {
                        UserIds[userId] = user;
                    }
                }
                var kuozhan = new Dictionary<string, string>();

                foreach (var item1 in aaaa.扩展字段)
                {
                    // 先检查 param.参数名 是否为空
                    if (string.IsNullOrEmpty(Convert.ToString(item1.参数名)))
                    {
                        dingcard.CreateErrorJson(108, "单聊酷应用投放参数：扩展字段参数不能为空");
                        break; // 停止执行
                    }
                    // 将参数名和参数值转换为字符串并添加到字典中
                    var key = (await dataContext.EvaluateFormulaAsync(item1.参数名))?.ToString() ?? string.Empty;
                    var value = (await dataContext.EvaluateFormulaAsync(item1.参数值))?.ToString() ?? string.Empty;

                    if (!string.IsNullOrEmpty(key) && value != null)
                    {
                        kuozhan[key] = value;
                    }
                }
                //组装对象数据并设置对象的对应值              
                var bbbType = bbb.GetType();
                var atUserIdsProperty = bbbType.GetProperty("atUserIds");
                var extensionProperty = bbbType.GetProperty("extension");

                if (atUserIdsProperty != null)
                {
                    atUserIdsProperty.SetValue(bbb, UserIds);
                }

                if (extensionProperty != null)
                {
                    extensionProperty.SetValue(bbb, kuozhan);
                }
                return bbb;
            }
            else
            {
                dingcard.CreateErrorJson(108, "单聊酷应用投放参数：数据内容不能为空");
                return null;
            }
        }
        //IM机器人单聊投放参数
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.DeliverCardRequest.DeliverCardRequestImRobotOpenDeliverModel> imjiqirendanliaotoufang(IServerCommandExecuteContext dataContext, imjqrtoufang aaaa)
        {
            string spaceType = null;
            string robotCode = null;
            var extension = new Dictionary<string, string>();
            if (aaaa != null)
            {
                if (aaaa.spaceType != null)
                {
                    spaceType = (await dataContext.EvaluateFormulaAsync(aaaa.spaceType))?.ToString() ?? string.Empty;
                }
                else
                {
                    dingcard.CreateErrorJson(110, "IM机器人单聊投放参数：spaeType参数不能为空");
                }
                if (aaaa.机器人robotCode != null)
                {
                    robotCode = (dataContext.EvaluateFormulaAsync(aaaa.机器人robotCode))?.ToString() ?? string.Empty;
                }
                else
                {
                    dingcard.CreateErrorJson(110, "IM机器人单聊投放参数：机器人robotCode不能为空");
                }
                foreach (var item1 in aaaa.扩展字段)
                {
                    // 先检查 param.参数名 是否为空
                    if (string.IsNullOrEmpty(Convert.ToString(item1.参数名)))
                    {
                        dingcard.CreateErrorJson(109, "群聊投放参数：扩展字段参数不能为空");
                        break; // 停止执行
                    }
                    // 将参数名和参数值转换为字符串并添加到字典中
                    var key = (await dataContext.EvaluateFormulaAsync(item1.参数名))?.ToString() ?? string.Empty;
                    var value = (await dataContext.EvaluateFormulaAsync(item1.参数值))?.ToString() ?? string.Empty;

                    if (!string.IsNullOrEmpty(key) && value != null)
                    {
                        extension[key] = value;
                    }
                }
                var imRobotOpenDeliverModel = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.DeliverCardRequest.DeliverCardRequestImRobotOpenDeliverModel
                {
                    SpaceType = spaceType,
                    RobotCode = robotCode,
                    Extension = extension,
                };
                return imRobotOpenDeliverModel;
            }
            else
            {
                dingcard.CreateErrorJson(110, "IM机器人单聊投放参数：数据内容不能为空");
                return null;
            }
        }
        //群聊投放参数
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.DeliverCardRequest.DeliverCardRequestImGroupOpenDeliverModel> qunliaotoufang(IServerCommandExecuteContext dataContext, qunliao aaaa)
        {
            string robotCode = null;
            var UserIds = new Dictionary<string, string>();
            var Recipients = new List<string>();
            var extension = new Dictionary<string, string>();
            if (aaaa != null)
            {
                if (aaaa.机器人robotCode != null)
                {
                    robotCode = (await dataContext.EvaluateFormulaAsync(aaaa.机器人robotCode))?.ToString() ?? string.Empty;
                }
                if (aaaa.AT用户 != null)
                {
                    foreach (var item1 in aaaa.AT用户)
                    {
                        // 先检查 param.参数名 是否为空
                        if (string.IsNullOrEmpty(Convert.ToString(item1.userId)))
                        {
                            dingcard.CreateErrorJson(109, "群聊投放参数：AT用户内容不能为空");
                            break; // 停止执行
                        }
                        // 将参数名和参数值转换为字符串并添加到字典中
                        var userId = (await dataContext.EvaluateFormulaAsync(item1.userId))?.ToString() ?? string.Empty;
                        var user = (await dataContext.EvaluateFormulaAsync(item1.用户名))?.ToString() ?? string.Empty;

                        if (userId != null && user != null)
                        {
                            UserIds[userId] = user;
                        }
                    }
                }
                if (aaaa.接收人userId列表 != null)
                {
                    foreach (var item1 in aaaa.接收人userId列表)
                    {
                        // 先检查 param.参数名 是否为空
                        if (string.IsNullOrEmpty(Convert.ToString(item1.userId)))
                        {
                            dingcard.CreateErrorJson(109, "群聊投放参数：接收人列表不能为空");
                            break; // 停止执行
                        }
                        // 将参数名和参数值转换为字符串并添加到字典中
                        var userId = (await dataContext.EvaluateFormulaAsync(item1.userId))?.ToString() ?? string.Empty;

                        if (userId != null)
                        {
                            Recipients.Add(userId);
                        }
                    }
                }

                if (aaaa.扩展字段 != null)
                {
                    foreach (var item1 in aaaa.扩展字段)
                    {
                        // 先检查 param.参数名 是否为空
                        if (string.IsNullOrEmpty(Convert.ToString(item1.参数名)))
                        {
                            dingcard.CreateErrorJson(109, "群聊投放参数：扩展字段参数不能为空");
                            break; // 停止执行
                        }
                        // 将参数名和参数值转换为字符串并添加到字典中
                        var key = (await dataContext.EvaluateFormulaAsync(item1.参数名))?.ToString() ?? string.Empty;
                        var value = (await dataContext.EvaluateFormulaAsync(item1.参数值))?.ToString() ?? string.Empty;

                        if (!string.IsNullOrEmpty(key) && value != null)
                        {
                            extension[key] = value;
                        }
                    }
                }
                var imGroupOpenDeliverModel = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.DeliverCardRequest.DeliverCardRequestImGroupOpenDeliverModel
                {
                    RobotCode = robotCode,
                    AtUserIds = UserIds,
                    Recipients = Recipients,
                    Extension = extension,
                };
                return imGroupOpenDeliverModel;

            }
            else
            {
                dingcard.CreateErrorJson(109, "群聊投放参数：数据内容不能为空");
                return null;
            }
        }
        //吊顶投放参数
        public static async Task<AlibabaCloud.SDK.Dingtalkcard_1_0.Models.DeliverCardRequest.DeliverCardRequestTopOpenDeliverModel> diaodingtoufang(IServerCommandExecuteContext dataContext, diaodingtoufang aaaa)
        {
            long expiredTimeMillis = 0;
            var UserIds = new List<string>();
            var Platforms = new List<string>();
            if (aaaa != null)
            {
                if (aaaa.过期时间戳 != null)
                {
                    string expiredTimeMillisString = (await dataContext.EvaluateFormulaAsync(aaaa.过期时间戳))?.ToString() ?? string.Empty;
                    if (long.TryParse(expiredTimeMillisString, out long cccc))
                    {
                        // 转换成功，使用 expiredTimeMillis
                        expiredTimeMillis = cccc;
                    }
                    else
                    {
                        dingcard.CreateErrorJson(111, "吊顶投放参数：无效的过期时间戳格式");
                    }
                }
                foreach (var item1 in aaaa.可以查看卡片的用户列表)
                {
                    // 先检查 param.参数名 是否为空
                    if (string.IsNullOrEmpty(Convert.ToString(item1.userId)))
                    {
                        dingcard.CreateErrorJson(111, "吊顶投放参数：可以查看卡片的用户列表不能为空");
                        break; // 停止执行
                    }
                    // 将参数名和参数值转换为字符串并添加到字典中
                    var userId = (await dataContext.EvaluateFormulaAsync(item1.userId))?.ToString() ?? string.Empty;

                    if (userId != null)
                    {
                        UserIds.Add(userId);
                    }
                }
                foreach (var item1 in aaaa.可以查看吊顶卡片的设备)
                {
                    // 先检查 param.参数名 是否为空
                    if (string.IsNullOrEmpty(Convert.ToString(item1.选择设备)))
                    {
                        dingcard.CreateErrorJson(111, "吊顶投放参数：可以查看卡片的用户列表不能为空");
                        break; // 停止执行
                    }
                    // 将参数名和参数值转换为字符串并添加到字典中
                    var ddd = item1.选择设备;

                    if (ddd != null)
                    {
                        Platforms.Add(ddd);
                    }
                }
                var topOpenDeliverModel = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.DeliverCardRequest.DeliverCardRequestTopOpenDeliverModel
                {
                    ExpiredTimeMillis = expiredTimeMillis,
                    UserIds = UserIds,
                    Platforms = Platforms,
                };
                return topOpenDeliverModel;
            }
            else
            {
                dingcard.CreateErrorJson(111, "吊顶投放参数：数据内容不能为空");
                return null;
            }
        }
        //协作投放参数
        public static async Task<DeliverCardRequest.DeliverCardRequestCoFeedOpenDeliverModel> xiezuotoufangcanshu(IServerCommandExecuteContext dataContext, xiezuotoufang aaaa)
        {
            string bbb = null;
            long ccc = 0;
            if (aaaa != null)
            {
                if (aaaa.业务标识 != null)
                {
                    bbb = (await dataContext.EvaluateFormulaAsync(aaaa.业务标识))?.ToString() ?? string.Empty;
                }
                else
                {
                    dingcard.CreateErrorJson(112, "协作投放参数：业务标识不能为空");
                }
                if (aaaa.排序时间 != null)
                {
                    string expiredTimeMillisString = (dataContext.EvaluateFormulaAsync(aaaa.排序时间))?.ToString() ?? string.Empty;
                    if (long.TryParse(expiredTimeMillisString, out long cccc))
                    {
                        // 转换成功，使用 expiredTimeMillis
                        ccc = cccc;
                    }
                    else
                    {
                        dingcard.CreateErrorJson(112, "协作投放参数：排序时间不能为空或时间戳格式错误");
                    }
                }
                var coFeedOpenDeliverModel = new DeliverCardRequest.DeliverCardRequestCoFeedOpenDeliverModel
                {
                    BizTag = bbb,
                    GmtTimeLine = ccc,
                };
                return coFeedOpenDeliverModel;

            }
            else
            {
                dingcard.CreateErrorJson(112, "协作投放参数：数据内容不能为空");
                return null;
            }
        }
        //文档投放参数
        public static async Task<DeliverCardRequest.DeliverCardRequestDocOpenDeliverModel> wendangtoufangcanshu(IServerCommandExecuteContext dataContext, userid aaaa)
        {
            string userid = null;
            if (aaaa != null)
            {
                if (aaaa.userId != null)
                {
                    userid = (await dataContext.EvaluateFormulaAsync(aaaa.userId))?.ToString() ?? string.Empty;
                }
                else
                {
                    dingcard.CreateErrorJson(113, "文档投放参数：userid不能为空");
                }
            }
            else
            {
                dingcard.CreateErrorJson(113, "文档投放参数：数据内容不能为空");
            }
            var docOpenDeliverModel = new DeliverCardRequest.DeliverCardRequestDocOpenDeliverModel
            {
                UserId = userid,
            };
            return docOpenDeliverModel;
        }      
        
    }
}
